﻿using HookupAppForm.Dto;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace HookupAppForm
{
    public partial class FormCate : Form
    {
        private string act = "api/services/app/HKP_Category/";
        private HKP_CategoryDto Item { get; set; }
        private APIClient client { get; set; }
        private CategoryTreeDto parentNode { get; set; }
        private string layout { get; set; }
        public FormCate(HKP_CategoryDto item, APIClient _client)
        {
            InitializeComponent();
            Item = item;
            client = _client;
        }

        private void FormCate_Load(object sender, EventArgs e)
        {
            string url = string.Format(act + "GetLayout");
            var result = client.Get(url);
            ItemsDto<string> res = JsonConvert.DeserializeObject<ItemsDto<string>>(result);
            if (res != null)
                comboBoxM.DataSource = res.result.items;
            LoadData();
        }

        private void LoadData()
        {
            string url = string.Format(act + "GetTreeList?layout={0}", layout);
            var result = client.Get(url);
            ListsDto<List<CategoryTreeDto>> res = JsonConvert.DeserializeObject<ListsDto<List<CategoryTreeDto>>>(result);
            if (res != null)
            {
                BindTreeView(res.result, treeViewM);
            }
            BindCombo();
        }
        private void BindCombo()
        {
            string url = string.Format(act + "GetList?layout={0}", layout);
            var result = client.Get(url);
            ItemsDto<HKP_CategoryDto> res = JsonConvert.DeserializeObject<ItemsDto<HKP_CategoryDto>>(result);
            if (res != null)
            {
                parentCate.DataSource = res.result.items;
                parentCate.DisplayMember = "CateName";
                parentCate.ValueMember = "Id";
            }
        }

        private void ComboBoxM_SelectedIndexChanged(object sender, EventArgs e)
        {
            layout = comboBoxM.SelectedValue.ToString();
            txtLayout.Text = layout;
            LoadData();
        }

        private void BindTreeView(List<CategoryTreeDto> nodes, TreeView tree)
        {
            try
            {
                tree.Nodes.Clear();
                foreach (var node in nodes)
                {
                    TreeNode rootNode = new TreeNode();
                    rootNode.Text = node.CateName;
                    rootNode.ToolTipText = node.CateIndex;
                    rootNode.Tag = node;
                    tree.Nodes.Add(rootNode);
                    if (node.Children != null)
                    {
                        foreach (var item in node.Children)
                        {
                            TreeNode obj = new TreeNode();
                            obj.Tag = item;
                            obj.Text = item.CateName;
                            obj.ToolTipText = item.CateIndex;
                            rootNode.Nodes.Add(obj);
                            BindTreeViewChild(item.Children, obj);
                        }
                    }
                }
                if (tree.Nodes.Count > 0)
                {
                    tree.Nodes[0].ExpandAll();
                    tree.SelectedNode = tree.Nodes[0];
                }

            }
            catch
            {
                
            }
        }
        private void BindTreeViewChild(List<CategoryTreeDto> childs, TreeNode fNode)
        {
            if (childs != null)
            {
                if (childs.Count == 0)
                {
                    return;
                }
                foreach (var item in childs)
                {
                    TreeNode node = new TreeNode();
                    node.Tag = item;
                    node.Text = item.CateName;
                    node.ToolTipText = item.CateIndex;
                    fNode.Nodes.Add(node);
                    //递归
                    BindTreeViewChild(item.Children, node);
                }
            }
        }
        private void TreeViewM_NodeMouseDoubleClick(object sender, TreeNodeMouseClickEventArgs e)
        {
            var obj = e.Node.Tag as CategoryTreeDto;
            Item = new HKP_CategoryDto
            {
                CateIndex = obj.CateIndex,
                CateName = obj.CateName,
                OrderIndex = obj.OrderIndex,
                ParentId = obj.ParentId,
                Layout = obj.Layout,
                Id = obj.Id
            };
            txtCateName.Text = Item.CateName;
            txtCateIndex.Text = Item.CateIndex;
            txtLayout.Text = Item.Layout;
            numOrderIndex.Value = Item.OrderIndex;
            parentCate.SelectedValue = Item.ParentId;
        }

        private void SaveToolStripButton_Click(object sender, EventArgs e)
        {
            string url = act + (Item != null ? "Update" : "Create");
            string result = string.Empty;
            string jsons = string.Empty;
            if (Item != null)
            {
                HKP_CategoryDto dto = new HKP_CategoryDto();
                dto.Id = Item.Id;
                dto.CateName = txtCateName.Text;
                dto.CateIndex = txtCateIndex.Text;
                dto.ParentId = Convert.ToInt32(parentCate.SelectedValue);
                dto.OrderIndex = Convert.ToInt32(numOrderIndex.Value);
                dto.Layout = txtLayout.Text;
                if (dto.ParentId == dto.Id)
                {
                    Utils.SysMsg("上级不能选择自身",MessageBoxIcon.Warning);
                    return;
                }
                jsons = JsonConvert.SerializeObject(dto);
                result = client.Put(url, jsons);
            }
            else
            {
                CreateHKP_CategoryDto dto = new CreateHKP_CategoryDto();
                dto.CateName = txtCateName.Text;
                dto.CateIndex = txtCateIndex.Text;
                dto.ParentId = Convert.ToInt32(parentCate.SelectedValue);
                dto.OrderIndex = Convert.ToInt32(numOrderIndex.Value);
                dto.Layout = txtLayout.Text;
                jsons = JsonConvert.SerializeObject(dto);
                result = client.Post(url, jsons);
            }

            if (Utils.ExceptionHandler(result) == null)
            {
                Utils.SysMsg("保存成功！", MessageBoxIcon.Information);
                var resp = Utils.SuccessHandler(result);
                Item = JsonConvert.DeserializeObject<HKP_CategoryDto>(resp.ToString());
                LoadData();
            }
        }

        private void DelToolStripButton_Click(object sender, EventArgs e)
        {
            if (MessageBox.Show("确定删除？", "系统", MessageBoxButtons.OKCancel, MessageBoxIcon.Question) == DialogResult.OK)
            {
                if (Item != null)
                {
                    string url = string.Format(act + "Delete?Id={0}", Item.Id);
                    string result = client.Delete(url);
                    if (Utils.ExceptionHandler(result) == null)
                    {
                        LoadData();
                    }
                }
            }
        }

        private void CopyToolStripButton_Click(object sender, EventArgs e)
        {
            if (Item != null)
            {
                string url = act + "Create";
                string jsons = JsonConvert.SerializeObject(Item);
                string result = client.Post(url, jsons);
                if (Utils.ExceptionHandler(result) == null)
                {
                    Utils.SysMsg("保存成功！");
                }
                LoadData();
            }
        }

        private void NewToolStripButton_Click(object sender, EventArgs e)
        {
            txtCateName.Text = "";
            txtCateIndex.Text = "";
            txtLayout.Text = layout;
            numOrderIndex.Value = 0;
            parentCate.SelectedValue = 0;
            Item = null;
            parentNode = null;
        }
    }
}
